package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ser.AnyGetterWriter;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.IApAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author PG
 * @since 2022-06-21
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements IApAuthorService {

    /**
     * 保存文章作者
     * @param author
     * @return
     */
    @Override
    public ResponseResult<ApAuthor> saveApAuthor(ApAuthor author) {
        //1. 保存作者 非空判断
        if (author == null || author.getUserId() == null || author.getWmUserId() == null){
            //1.1 为空, 返回 无效参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2. 判断是不是已经有记录  判断 user_id 对应的记录
        //2.1 根据 user_id 查询
        LambdaQueryWrapper<ApAuthor> query = new LambdaQueryWrapper<>();
        query.eq(ApAuthor::getUserId, author.getUserId());
        ApAuthor one = this.getOne(query);
        //3. 如果能查到, 说明作者存在, 直接返回
        if (one != null){
            return ResponseResult.okResult(one);
        }
        //新增, 返回新增的作者信息
        author.setCreatedTime(new Date());
        // 存到Ap_author 表里
        this.save(author);
        return null;
    }
}
